package com.tsingtree.modular.service.impl;

import com.tsingtree.modular.entity.*;
import com.tsingtree.modular.mapper.NanSellerMapper;
import com.tsingtree.modular.model.params.NanSellerParam;
import com.tsingtree.modular.model.params.NanSellerParam;
import com.tsingtree.modular.model.result.NanSellerResult;
import com.tsingtree.modular.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tsingtree.base.pojo.page.LayuiPageFactory;
import com.tsingtree.roses.core.util.ToolUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;


/**
 * <p>
 * 运营组 服务实现类
 * </p>
 *
 * @author jagger
 * @since 2023-08-29
 */
@Service
public class NanSellerServiceImpl extends ServiceImpl<NanSellerMapper, NanSeller> implements NanSellerService {

    @Autowired
    private MercUserService mercUserService;
    @Autowired
    private MercRoleService mercRoleService;
    @Autowired
    private MercMenuService mercMenuService;
    @Autowired
    private MercRelationService mercRelationService;

    @Override
    public void delete(NanSellerParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(NanSellerParam param, MercUser newUser, MercRole mercRole){
        NanSeller entity = getEntity(param);
        this.save(entity);
        mercRole.setTenantId(entity.getId());
        mercRoleService.save(mercRole);
        newUser.setTenantId(entity.getId());
        newUser.setRoleId(mercRole.getRoleId().toString());
        mercUserService.save(newUser);
        //创建角色和权限的关联表
        List<MercMenu> list = mercMenuService.list();
        List<MercRelation> relations = new ArrayList<>();
        list.forEach(mercMenu -> {
            MercRelation mercRelation = new MercRelation();
            mercRelation.setRoleId(mercRole.getRoleId());
            mercRelation.setMenuId(mercMenu.getMenuId());
            relations.add(mercRelation);
        });
        if(!CollectionUtils.isEmpty(relations)){
            mercRelationService.saveBatch(relations);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NanSellerParam param, List<MercUser> updateMercUsers){
        if(!CollectionUtils.isEmpty(updateMercUsers)){
            mercUserService.updateBatchById(updateMercUsers);
        }
        NanSeller oldEntity = getOldEntity(param);
        NanSeller newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NanSellerResult findBySpec(NanSellerParam param){
        return null;
    }

    @Override
    public List<NanSellerResult> findListBySpec(NanSellerParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NanSellerParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(NanSellerParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NanSeller getOldEntity(NanSellerParam param) {
        return this.getById(getKey(param));
    }

    private NanSeller getEntity(NanSellerParam param) {
        NanSeller entity = new NanSeller();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
